//
//  UserCenterIndexViewController.m
//  ELife
//
//  Created by  刘奇男 on 11-6-15.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "UserCenterIndexViewController.h"
#import "Constants.h"
#import "ELifeAppDelegate.h"
#import "ASIFormDataRequest.h"
#import "GTMBase64.h"
#import <CommonCrypto/CommonDigest.h>
#import <Foundation/Foundation.h>
#include "GDataXMLNode.h"


@implementation UserCenterIndexViewController

@synthesize userCenterList, userCenterKey, tableView;

- (void)dealloc
{
	[userCenterList release];
    [userCenterKey release];
    
	[super dealloc];
}

- (void)viewDidLoad
{
	// Make the title of this page the same as the title of this app
	self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
    
	self.userCenterList = [NSMutableArray array];
	
	// We will lazily create our view controllers as the user requests them (at a later time),
	// but for now we will encase each title an explanation text into a NSDictionary and add it to a mutable array.
	// This dictionary will be used by our table view data source to populate the text in each cell.
	//
	// When it comes time to create the corresponding view controller we will replace each NSDictionary.
	//
	// If you want to add more pages, simply call "addObject" on "menuList"
	// with an additional NSDictionary.  Note we use NSLocalizedString to load a localized version of its title.
    if (!userCenterKey)
	{
		userCenterKey = [[NSArray alloc] initWithObjects:@"UserCenter1", @"UserCenter2", @"UserCenter3", @"UserCenter4", @"UserCenter5", nil];
    }
	
    for (NSString *key in userCenterKey)
	{
		[self.userCenterList addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        NSLocalizedString([key stringByAppendingString:@"Title"], @""), kTitleKey,
                                        NSLocalizedString([key stringByAppendingString:@"ClassName"], @""), kClassNameKey,
                                        nil]];	
    }
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    
    UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
	temporaryBarButtonItem.title = @"返回";
	self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
	[temporaryBarButtonItem release];
    
    
    UIImageView *backImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]];
    [self.tableView setBackgroundView:backImg];
    [backImg release];
}

-(void) viewWillAppear:(BOOL)animated
{
}

- (void)viewDidUnload
{
	self.userCenterKey = nil;
    self.userCenterList = nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return userCenterList.count;
}

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *kCellIdentifier = @"UserCenterCellID";
	UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:kCellIdentifier];
	if (cell == nil)
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCellIdentifier] autorelease];
		
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.opaque = NO;
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.highlightedTextColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
	
    // get the view controller's info dictionary based on the indexPath's row
    NSDictionary *dataDictionary = [userCenterList objectAtIndex:indexPath.row];
    cell.textLabel.text = [dataDictionary valueForKey:kTitleKey];
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *rowData = [self.userCenterList objectAtIndex:indexPath.row];
	UIViewController *targetViewController = [rowData objectForKey:kViewControllerKey];
	if (!targetViewController)
	{
        NSString *viewName = [rowData objectForKey:kClassNameKey];
        
        targetViewController = [[NSClassFromString([viewName stringByAppendingString:@"ViewController"]) alloc] initWithNibName:[viewName  stringByAppendingString:@"View"] bundle:nil];
        
        [rowData setValue:targetViewController forKey:kViewControllerKey];
        
        [targetViewController release];
        
    }
    [self.navigationController pushViewController:targetViewController animated:YES]; 
}

@end
